/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28318
 * Date: 2025-10-31
 * Time: 16:25
 */

/**
 * 存储系统资源分配的状态数据
 */
public class SystemState {
    private int n; // 进程数量
    private int m; // 资源类型数量
    private int[][] max; // 最大需求矩阵
    private int[][] allocation; // 已分配矩阵
    private int[][] need; // 需求矩阵
    private int[] available; // 可用资源向量

    public SystemState(int n, int m) {
        this.n = n;
        this.m = m;
        this.max = new int[n][m];
        this.allocation = new int[n][m];
        this.need = new int[n][m];
        this.available = new int[m];
    }

    // 计算需求矩阵 Need = Max - Allocation
    public void calculateNeed() {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                need[i][j] = max[i][j] - allocation[i][j];
            }
        }
    }

    // getter和setter方法
    public int getN() { return n; }
    public int getM() { return m; }
    public int[][] getMax() { return max; }
    public int[][] getAllocation() { return allocation; }
    public int[][] getNeed() { return need; }
    public int[] getAvailable() { return available; }

    public void setMax(int[][] max) { this.max = max; }
    public void setAllocation(int[][] allocation) { this.allocation = allocation; }
    public void setNeed(int[][] need) { this.need = need; }
    public void setAvailable(int[] available) { this.available = available; }

    // 显示当前系统状态
    public void displayState() {
        System.out.println("\n===== 当前系统状态 =====");

        // 显示可用资源
        System.out.println("可用资源 Available:");
        for (int j = 0; j < m; j++) {
            System.out.print("R" + (j + 1) + "=" + available[j] + " ");
        }
        System.out.println("\n");

        // 显示进程资源信息
        System.out.println("进程\tMax\t\t\tAllocation\tNeed");
        System.out.println("\tR1 R2 R3\tR1 R2 R3\tR1 R2 R3");

        for (int i = 0; i < n; i++) {
            System.out.print("P" + i + "\t");

            // 显示Max
            for (int j = 0; j < m; j++) {
                System.out.print(max[i][j] + "  ");
            }
            System.out.print("\t");

            // 显示Allocation
            for (int j = 0; j < m; j++) {
                System.out.print(allocation[i][j] + "  ");
            }
            System.out.print("\t");

            // 显示Need
            for (int j = 0; j < m; j++) {
                System.out.print(need[i][j] + "  ");
            }
            System.out.println();
        }
        System.out.println("=======================\n");
    }
}